//
//  LJJCollectionViewCell.swift
//  LJJCycleDemo
//
//  Created by liujunjie on 2019/1/29.
//  Copyright © 2019 junjie.liu. All rights reserved.
//

import UIKit

class LJJCollectionViewCell: UICollectionViewCell {
    
    /** 标题背景色 */
    var titleLabelBackgroundColor: UIColor? {
        didSet {
            self._titleLabel?.backgroundColor = titleLabelBackgroundColor
        }
    }
    
    /** 标题字体色 */
    var titleLabelTextColor: UIColor? {
        didSet {
            self._titleLabel?.textColor = titleLabelTextColor
        }
    }
    
    /** 标题字号 */
    var titleLabelTextFont: UIFont? {
        didSet {
            self._titleLabel?.font = titleLabelTextFont
        }
    }
    
    /** 文字 */
    var title: String? {
        didSet {
            self._titleLabel?.isHidden = false
            self._titleLabel?.text = " \(String(describing: title))"
        }
    }
    
    /** 对齐方式 */
    var titleLabelTextAlignment: NSTextAlignment? {
        didSet {
          
            self._titleLabel?.textAlignment = titleLabelTextAlignment ?? NSTextAlignment.center
        }
    }
    
    var imageView : UIImageView?
    var titleLabelHeight: CGFloat?
    var hasConfigured: Bool?
    var onlyDisplayText: Bool?/** 只展示文字轮播 */
    fileprivate var _titleLabel : UILabel?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupImageView()
        setupTitleLabel()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LJJCollectionViewCell {
    
    func setupImageView()  {
        let imageView = UIImageView()
        self.imageView = imageView
        self.contentView.addSubview(imageView)
    }
    
    func setupTitleLabel()  {
        let label = UILabel()
        _titleLabel = label
        _titleLabel?.isHidden = true
        self.contentView.addSubview(_titleLabel ?? UILabel())
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if self.onlyDisplayText == true {
            _titleLabel?.frame = self.bounds
        }else{
            self.imageView?.frame = self.bounds
            let titleLabelW: CGFloat = self.ljj_width
            let titleLabelH: CGFloat = CGFloat(self.titleLabelHeight ?? 60)
            let titleLabelX: CGFloat = 0
            let titleLabelY: CGFloat = self.ljj_height - CGFloat(titleLabelH)
            self._titleLabel?.frame = CGRect(x: titleLabelX, y: titleLabelY, width: titleLabelW, height: titleLabelH)
        }
    }
}

